package com.thinkgem.jeesite.modules.bls.service;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.modules.bls.dao.LoanDao;
import com.thinkgem.jeesite.modules.bls.entity.Loan;

/**
 * 信贷申请状态处理器
 * <p>
 * 设置审批完成状态 
 * </p>
 * <p>
 * 使用Spring代理，可以注入Bean，管理事物
 * </p>
 * 
 * @author edpdennis
 */
@Component
@Transactional
public class ReportBackEndProcessor implements TaskListener {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1828174954086385656L;

	@Autowired
	LoanDao loanDao;

	@Autowired
	RuntimeService runtimeService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.
	 * delegate.DelegateTask)
	 */
	public void notify(DelegateTask delegateTask) {
		String processInstanceId = delegateTask.getProcessInstanceId();
		ProcessInstance processInstance = runtimeService
				.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		Loan loan = loanDao.findOne(new Long(processInstance.getBusinessKey()));

		Object ischeck = delegateTask.getVariable("ischeck");
		loan.setIscheck((String) ischeck);

		loanDao.save(loan);
	}

}
